# -*- coding: utf-8 -*-
import scrapy, re
from yundama import getma


class BaweiSpider(scrapy.Spider):
    name = 'bawei'

    def start_requests(self):
        yield scrapy.Request(url='http://172.16.10.111/exam/login.do', callback=self.requests_yzm)

    def requests_yzm(self, response):
        yield scrapy.Request(url='http://172.16.10.111/exam/validationCode.do', callback=self.parse_yzm)

    def parse_yzm(self, response):
        with open('yzm.jpg', 'wb+') as f:
            f.write(response.body)
        yzm = getma()
        formdata = {
            'username': '18380100007',
            'password': '04110015',
            'validationCode': yzm
        }
        yield scrapy.FormRequest(url='http://172.16.10.111/exam/gologin.do', formdata=formdata, callback=self.parse)

    def parse(self, response):
        # print(response.text)
        name = re.findall(r'<div id="login-info-user">(.*?)<', response.text, re.S)[0].strip()
        print(name)
